import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class DataBaseFramework {

	private HashMap<String,PersistentObject> persistentObjects;
	
	public DataBaseFramework() {
		try {
		      ObjectInputStream stream = new ObjectInputStream(new FileInputStream("restaurant.db"));
		      setPersistentObjects((HashMap<String, PersistentObject>) (stream.readObject()));
		      stream.close();
		    }
		    catch (IOException ex) {
					ex.printStackTrace();
		    }
		    catch (ClassNotFoundException ex) {
					ex.printStackTrace();
		    }
	}
	
	public void closeDB() {
		try {
		      ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("restaurant.db"));
		      stream.writeObject(getPersistentObjects());
		      stream.close();
		    }
		    catch (IOException ex) {
					ex.printStackTrace();
		    }
	}
	
	public PersistentObject getObject(String name) {
		return getPersistentObjects().get(name);
	}
	
	public void add(PersistentObject pObj) {
		getPersistentObjects().put(pObj.key(), pObj);
	}
	
	public void delete(String name) {
		getPersistentObjects().remove(name);
	}

	public void setPersistentObjects(HashMap<String,PersistentObject> persistentObjects) {
		this.persistentObjects = persistentObjects;
	}

	public HashMap<String,PersistentObject> getPersistentObjects() {
		return persistentObjects;
	}

}